package cn.gov.pbc.gd.credit.user.service.menu.impl;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.liu.jbase.ResultVo;
import com.liu.jbase.annotation.LogAnnotation;
import com.liu.jbase.service.impl.BaseServiceImpl;
import com.liu.tools.LogUtil;
import com.liu.tools.StrUtil;

import cn.gov.pbc.gd.credit.api.menu.IMenuApi;
import cn.gov.pbc.gd.credit.common.constant.CodeConstant;
import cn.gov.pbc.gd.credit.common.constant.CommonConstant;
import cn.gov.pbc.gd.credit.common.constant.LogConstant;
import cn.gov.pbc.gd.credit.common.service.redis.IRedisService;
import cn.gov.pbc.gd.credit.user.service.menu.IMenuService;
import cn.gov.pbc.gd.credit.vo.menu.MenuVo;
import cn.gov.pbc.gd.credit.vo.menu.ResMenuVo;

@Service
public class MenuServiceImpl extends BaseServiceImpl<ResMenuVo, MenuVo, IMenuApi> implements IMenuService{

	private static Logger logger = LoggerFactory.getLogger(MenuServiceImpl.class);
	
	@Autowired
	private IMenuApi menuApi;
	
	@Autowired
	public IRedisService redisService;
	
	@Override
	public ResultVo<List<ResMenuVo>> treeMenuList(MenuVo vo, HttpServletRequest request) {
		
		ResultVo<List<ResMenuVo>> resultVo = new ResultVo<>();
		
		List<ResMenuVo> list = menuApi.treeMenuList(vo);
		
		resultVo.setCode(CodeConstant.SUCCESS);
		resultVo.setData(list);
		return resultVo;
	}
	
	@LogAnnotation(moduleName=LogConstant.MODULE_MENU, option=LogConstant.ADD, logType=LogConstant.LOG_OPERATE, content="添加权限管理")
	@Override
	public ResultVo<ResMenuVo> add(MenuVo record, HttpServletRequest request) {
		
		ResultVo<ResMenuVo> resultVo = super.add(record, request);
		return resultVo;
	}
	@LogAnnotation(moduleName=LogConstant.MODULE_MENU, option=LogConstant.UPDATE, logType=LogConstant.LOG_OPERATE, content="修改权限管理")
	@Override
	public ResultVo<ResMenuVo> update(MenuVo record, HttpServletRequest request) {
		
		ResultVo<ResMenuVo> resultVo = super.update(record, request);
		
		return resultVo;
	}
	@LogAnnotation(moduleName=LogConstant.MODULE_MENU, option=LogConstant.DEL, logType=LogConstant.LOG_OPERATE, content="删除权限管理")
	@Override
	public ResultVo<ResMenuVo> delete(MenuVo record, HttpServletRequest request) {
		
		ResultVo<ResMenuVo> resultVo = super.delete(record, request);
		return resultVo;
	}

	
	
	@Override
	public ResultVo<List<ResMenuVo>> cacheTreeMenu(MenuVo vo, HttpServletRequest request) {

		if(vo == null){
			vo = new MenuVo();
		}
		
		vo.setStatus(1);
		
		List<ResMenuVo> list = menuApi.treeMenuList(vo);
		
		if(list != null && list.size() > 0){
			redisService.delKey(CommonConstant.CACHE_TREE_MENU);
			redisService.set(CommonConstant.CACHE_TREE_MENU, JSON.toJSONString(list));
			logger.info(LogUtil.msg("MenuServiceImpl:cacheTreeMenu", "树形权限列表缓存成功"));
		} else {
			logger.info(LogUtil.msg("MenuServiceImpl:cacheTreeMenu", "树形权限列表为空"));
		}
		
		ResultVo<List<ResMenuVo>> resultVo = new ResultVo<>();
		resultVo.setCode(CodeConstant.SUCCESS);
		return resultVo;
	}

	@Override
	public ResultVo<List<ResMenuVo>> getCacheTreeMenu(MenuVo vo, HttpServletRequest request) {

		String listStr = redisService.get(CommonConstant.CACHE_TREE_MENU);
		
		if(StrUtil.isNull(listStr)){
			
			cacheTreeMenu(new MenuVo(), request);
			listStr = redisService.get(CommonConstant.CACHE_TREE_MENU);
		}
		
		ResultVo<List<ResMenuVo>> resultVo = new ResultVo<>();
		
		if(StrUtil.isNull(listStr)){
			resultVo.setCode(CodeConstant.FAIL);
			logger.info(LogUtil.msg("MenuServiceImpl:getCacheTreeMenu", "树形权限列表为空"));
			return resultVo;
		}
		
		List<ResMenuVo> list = JSON.parseArray(listStr, ResMenuVo.class);
		
		resultVo.setCode(CodeConstant.SUCCESS);
		resultVo.setData(list);
		
		return resultVo;
	}
	
	
	
}
